﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Extensions;

namespace Common.UI.Controls.Utils
{
	public abstract class BarChartItemsSourceBase<T>
	{
		public Guid Id { get; private set; }
		public IList<BarItem<T>> FrontItems { get; private set; }
		public IList<BarItem<T>> BackItems { get; private set; }
		public T Max { get; private set; }
		public Int32 Count { get; private set; }

		public BarChartItemsSourceBase(IList<BarItem<T>> frontItems, IList<BarItem<T>> backItems)
		{
			FrontItems = frontItems ?? new List<BarItem<T>>();
			BackItems = backItems ?? new List<BarItem<T>>();

			Max = GetMax(GetMax(FrontItems.ToArray()), GetMax(BackItems.ToArray()));
			Count = Math.Max(FrontItems.IsNullOrEmpty() ? 0 : FrontItems.Count, BackItems.IsNullOrEmpty() ? 0 : BackItems.Count);
			Id = Guid.NewGuid();
		}


		public IList<TRes> ForEach<TRes>(System.Action<IList<TRes>, BarItem<T>, BarItem<T>, Int32, Boolean> act)
		{
			var res = new List<TRes>();
			for (Int32 i = 0; i < Count; i++)
			{
				var frontItem = FrontItems.IsAvailableIndex(i) ? FrontItems[i] : new BarItem<T>(Guid.Empty, default(T));
				var backItem = BackItems.IsAvailableIndex(i) ? BackItems[i] : new BarItem<T>(Guid.Empty, default(T));
				act(res, frontItem, backItem, i, i == Count);
			}
			return res;
		}

		public abstract T GetMax(params BarItem<T>[] values);

		public abstract T GetMax(T item1, T item2);

	}
}
